package com.power.ding.dao.impl;

import com.alibaba.fastjson.JSON;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.ding.dao.Di_deptDaoCustom;
import com.power.ding.model.Di_dept;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;
import java.util.Map;

/**
 * 部门信息 持久层实现(hibernate)
 */

@Slf4j
@Repository
public class Di_deptDaoImpl implements Di_deptDaoCustom {

    private final EntityManager entityManager;

    public Di_deptDaoImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public List<Di_dept> queryListByUserid(String userid) {
        StringBuffer sql = new StringBuffer("select b.*, a.ismain from di_userdept a left join di_dept b on a.orgid = b.id where a.userid = ?");
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        query.setParameter(1, userid);
        List<Map> list = query.getResultList();
        return JSON.parseArray(JSON.toJSONString(list), Di_dept.class);
    }
}
